package com.kylezhang.runner.web.interceptor;

import com.kylezhang.runner.core.domain.Customer;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Created by KyleZhang
 * 2018/7/2 17:08
 */
public class LoginInterceptor extends HandlerInterceptorAdapter {

    //preHandle在业务处理器处理请求之前被调用。预处理，可以进行编码、安全控制等处理；
    @Override
 	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //获取session
        HttpSession session = request.getSession();
        Customer customer = (Customer) session.getAttribute("customer");
        //判断用户ID是否存在，不存在就跳转到登录界面
        if (customer!=null){
            return super.preHandle(request, response, handler);
        }else {
            response.sendRedirect("/publiz/login");
            return false;
        }
    }
}
